﻿using System;
using System.Windows;

namespace AvalonDock
{
    internal class OverlayWindowDockingButton : IDropSurface
    {
        #region Constants and Fields

        private readonly FrameworkElement _btnDock;

        private readonly OverlayWindow _owner;

        private bool _enabled = true;

        #endregion

        #region Constructors and Destructors

        public OverlayWindowDockingButton(FrameworkElement btnDock, OverlayWindow owner)
            : this(btnDock, owner, true)
        {
        }

        public OverlayWindowDockingButton(FrameworkElement btnDock, OverlayWindow owner, bool enabled)
        {
            if (btnDock == null)
            {
                throw new ArgumentNullException("btnDock");
            }
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            _btnDock = btnDock;
            _owner = owner;
            Enabled = enabled;
        }

        #endregion

        #region Public Properties

        public bool Enabled
        {
            get
            {
                return _enabled;
            }
            set
            {
                _enabled = value;
                _btnDock.Visibility = _enabled ? Visibility.Visible : Visibility.Hidden;
            }
        }

        #endregion

        #region Explicit Interface Properties

        bool IDropSurface.IsSurfaceVisible
        {
            get
            {
                return (_owner.IsLoaded && _btnDock != null);
            }
        }

        Rect IDropSurface.SurfaceRectangle
        {
            get
            {
                if (!(this as IDropSurface).IsSurfaceVisible)
                {
                    return Rect.Empty;
                }

                if (PresentationSource.FromVisual(_btnDock) == null)
                {
                    return Rect.Empty;
                }

                return new Rect(
                    HelperFunc.PointToScreenWithoutFlowDirection(_btnDock, new Point()),
                    new Size(_btnDock.ActualWidth, _btnDock.ActualHeight));
            }
        }

        #endregion

        #region Explicit Interface Methods

        void IDropSurface.OnDragEnter(Point point)
        {
            if (!Enabled)
            {
                return;
            }

            _owner.OnDragEnter(this, point);
        }

        void IDropSurface.OnDragLeave(Point point)
        {
            if (!Enabled)
            {
                return;
            }

            _owner.OnDragLeave(this, point);
        }

        void IDropSurface.OnDragOver(Point point)
        {
            if (!Enabled)
            {
                return;
            }

            _owner.OnDragOver(this, point);
        }

        bool IDropSurface.OnDrop(Point point)
        {
            if (!Enabled)
            {
                return false;
            }

            return _owner.OnDrop(this, point);
        }

        #endregion
    }
}